#!/bin/bash
# 初始化项目脚本

#yum包更新到最新
yum -y update

#编译工具的安装：
yum -y install gcc
yum -y install gcc-c++

#安装lsof工具
yum install -y lsof

#安装需要的软件包
yum install -y yum-utils device-mapper-persistent-data lvm2

#设置yum源
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo

#安装Docker，出现输入的界面都按y
yum install -y docker-ce

#查看docker版本，验证是否验证成功
docker -v

#创建名称为www的文件夹
# mkdir /home/app/www
mkdir /home/www

#安装Git的工具
yum -y install git

#进入/home/www的文件夹
cd /home/www

#启动git
git init

#启动docker服务
systemctl start docker

#将docker镜像源修改为（国内）阿里云的镜像地址：
touch /etc/docker/daemon.json

echo "{
  \"registry-mirrors\": [\"https://9cpn8tt6.mirror.aliyuncs.com\"]
}" >>  /etc/docker/daemon.json

#创建一个docker的network
docker network create my_net

#在dockerHub拉取最新的nginx镜像
docker pull nginx

#在dockerHub拉取最新的mysql8
docker pull mysql

#在dockerHub拉取版本号为7.3.24的php
docker pull php:7.3-fpm

#在dockerHub拉取最新的redis
docker pull redis

#安装docker-compose
yum -y install python3-pip
pip3 install --upgrade pip
pip3 install docker-compose
docker-compose --version

#关闭所有容器
docker stop $(docker ps -a | awk '{ print $1}' | tail -n +2)

#删除所有容器
docker rm $(docker ps -a | awk '{ print $1}' | tail -n +2)

#编写docker-compose文件
echo "
version: '3'

services:

  php:
    working_dir: /usr/share/nginx/html
    image: php:7.3-fpm
    container_name: my_php
    restart: always
    volumes:
      - /home/www/:/usr/share/nginx/html/
    ports:
      - 9000:9000
    networks:
      - my_net
    privileged: true

  nginx:
    working_dir: /usr/share/nginx/html
    container_name: my_nginx
    image: nginx:latest
    restart: always
    ports:
      - 80:80
      - 443:443
    networks:
      - my_net
    volumes:
      - /home/nginx/conf/:/etc/nginx/conf.d/
      - /home/www/:/usr/share/nginx/html/
    privileged: true

  db:
    container_name: mysql
    image: mysql
    command: --default-authentication-plugin=mysql_native_password
    restart: always
    privileged: true
    ports:
      - 3306:3306
    environment:
      MYSQL_ROOT_PASSWORD: 144090060
      MYSQL_DATABASE: PS_RD
      MYSQL_USER: hyk
      MYSQL_PASSWORD: 144090060
      TZ: Asia/Shanghai
    networks:
      - my_net

  redis:
    container_name: my_redis
    image: redis:latest
    restart: always
    ports:
      - 6379:6379
    privileged: true
    networks:
      - my_net

networks:
  my_net:
" >> /home/docker-compose.yaml

#启动docker-compose
cd /home && docker-compose up -d

#安装composer
docker exec -i my_php /bin/bash -c " php -r \"copy('https://install.phpcomposer.com/installer', 'composer-setup.php');\""

docker exec -i my_php /bin/bash -c "php composer-setup.php"

docker exec -i my_php /bin/bash -c "mv composer.phar /usr/local/bin/composer"

docker exec -i my_php /bin/bash -c "composer config -g repo.packagist composer https://mirrors.aliyun.com/composer/"



docker exec -i my_php /bin/bash -c "mv /etc/apt/sources.list /etc/apt/sources.list.bak"

#修改my_php容器内apt-get镜像源
docker exec -i my_php /bin/bash -c "echo \"
deb http://mirrors.aliyun.com/debian/ stretch main non-free contrib
deb-src http://mirrors.aliyun.com/debian/ stretch main non-free contrib
deb http://mirrors.aliyun.com/debian-security stretch/updates main
deb-src http://mirrors.aliyun.com/debian-security stretch/updates main
deb http://mirrors.aliyun.com/debian/ stretch-updates main non-free contrib
deb-src http://mirrors.aliyun.com/debian/ stretch-updates main non-free contrib
deb http://mirrors.aliyun.com/debian/ stretch-backports main non-free contrib
deb-src http://mirrors.aliyun.com/debian/ stretch-backports main non-free contrib
\" >> /etc/apt/sources.list
"

#更新apt-get
docker exec -i my_php /bin/bash -c "apt-get update"

docker exec -i my_php /bin/bash -c "apt-get -y install zip unzip git"



#判断PS_RD项目是否存在
if  [ ! -d "/home/www/PS_RD" ]; then
  cd /home/www
  git init
  git config --global user.name "黄永铿"
  git config --global user.email "675666296@qq.com"
  git clone https://gitee.com/Henry_Wong_php/PS_RD.git
else
  cd /home/www/PS_RD
  git pull origin master
fi

docker exec -i my_php /bin/bash -c "cd /usr/share/nginx/html/PS_RD && composer up"

if  [ ! -d "/home/www/PS_RD/.env" ]; then
    echo "
    APP_NAME=Laravel
    APP_ENV=local
    APP_KEY=base64:o3AOnqIiYKISSu06kEoRepNl3KFr7j4oSyWhIFskMnw=
    APP_DEBUG=true
    APP_URL=http://localhost

    LOG_CHANNEL=stack
    LOG_DEPRECATIONS_CHANNEL=null
    LOG_LEVEL=debug

    DB_CONNECTION=mysql
    DB_HOST=mysql
    DB_PORT=3306
    DB_DATABASE=PS_RD
    DB_USERNAME=root
    DB_PASSWORD=144090060

    BROADCAST_DRIVER=log
    CACHE_DRIVER=file
    FILESYSTEM_DRIVER=local
    QUEUE_CONNECTION=sync
    SESSION_DRIVER=file
    SESSION_LIFETIME=120

    MEMCACHED_HOST=127.0.0.1

    REDIS_HOST=8.130.9.76
    REDIS_PASSWORD=null
    REDIS_PORT=6379

    MAIL_MAILER=smtp
    MAIL_HOST=mailhog
    MAIL_PORT=1025
    MAIL_USERNAME=null
    MAIL_PASSWORD=null
    MAIL_ENCRYPTION=null
    MAIL_FROM_ADDRESS=null
    MAIL_FROM_NAME=\"\${APP_NAME}\"

    AWS_ACCESS_KEY_ID=
    AWS_SECRET_ACCESS_KEY=
    AWS_DEFAULT_REGION=us-east-1
    AWS_BUCKET=
    AWS_USE_PATH_STYLE_ENDPOINT=false

    PUSHER_APP_ID=
    PUSHER_APP_KEY=
    PUSHER_APP_SECRET=
    PUSHER_APP_CLUSTER=mt1

    MIX_PUSHER_APP_KEY=\"\${PUSHER_APP_KEY}\"
    MIX_PUSHER_APP_CLUSTER=\"\${PUSHER_APP_CLUSTER}\"

    CNAME = http://cdn.henrywong01.top
    Ak = Ebw9P7JauxQmgrEi23cZmHZmGNr4K2LNnURiyc7m
    SK = YF76U69EHiHVhqx_o0dHHvIesZT2KRlpm3WWiCOM
    BUCKET = pshape

    " >> /home/www/PS_RD/.env
else
    echo ".env文件已存在"
fi

#编写nginx文件
mkdir /home/nginx/conf/
echo "
server {
        listen       443 ssl;
        server_name www.henrywong01.top;
        root    /usr/local/nginx/html/PS_RD/public;
        index   index.html index.php index.htm;

        ssl_certificate /usr/share/nginx/html/PS_RD/public/cert/9255844_www.henrywong01.top.pem;
        ssl_certificate_key /usr/share/nginx/html/PS_RD/public/cert/9255844_www.henrywong01.top.key;
        ssl_session_timeout 5m;
        #表示使用的加密套件的类型
        ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
        #表示使用的TLS协议的类型，您需要自行评估是否配置TLSv1.1协议。
        ssl_protocols TLSv1.1 TLSv1.2 TLSv1.3;
        ssl_prefer_server_ciphers on;

        #项目下允许访问的静态资源文件类型
        location ~* ^.+\.(jpg|jpeg|gif|png|bmp|sh|yaml|pem|key|conf|css|less|sass)$ {
        		access_log off;
        		root /usr/share/nginx/html/PS_RD/public;
        		index  index.html index.htm;
        		expires 30d;
        		break;
        }

        location / {
                if (!-e \$request_filename){
                rewrite ^/(.*)\$ /index.php?p=\$1 last;
                }
        }

        location ~ \.php\$ {
                    root     /usr/share/nginx/html/PS_RD;
                    fastcgi_pass   my_php:9000;
                    fastcgi_index  index.php;
                    fastcgi_param  SCRIPT_FILENAME  /usr/share/nginx/html/PS_RD/public\$fastcgi_script_name;
                    include        fastcgi_params;
                  }
}

server {
    listen 80;
    #填写证书绑定的域名
    server_name www.henrywong01.top;
    #将所有HTTP请求通过rewrite指令重定向到HTTPS。
    rewrite ^(.*)$ https://\$host\$1;
    location / {
        index index.html index.htm;
    }
}
" >> /home/nginx/conf/default.conf
docker restart my_nginx

#下载并安装gd扩展
docker exec -i my_php /bin/bash -c "docker-php-ext-install gd"
docker exec -i my_php /bin/bash -c "docker-php-ext-enable gd"
#下载并安装pdo和pdo_mysql扩展
docker exec -i my_php /bin/bash -c "docker-php-ext-install pdo pdo_mysql"
docker exec -i my_php /bin/bash -c "docker-php-ext-enable pdo pdo_mysql"

#下载并安装redis扩展
docker exec -i my_php /bin/bash -c "apt-get install wget"
docker exec -i my_php /bin/bash -c "docker-php-source extract"
docker exec -i my_php /bin/bash -c "cd /usr/local/include/php/ext && wget http://pecl.php.net/get/redis-4.3.0.tgz"
docker exec -i my_php /bin/bash -c "cd /usr/local/include/php/ext && tar -zxvf redis-4.3.0.tgz"
docker exec -i my_php /bin/bash -c "cd /usr/local/include/php/ext/redis-4.3.0 && /usr/local/bin/phpize"
docker exec -i my_php /bin/bash -c "mv /usr/local/include/php/ext/redis-4.3.0 /usr/src/php/ext/redis"
docker exec -i my_php /bin/bash -c "cd /usr/src/php/ext/redis && docker-php-ext-configure redis"
docker exec -i my_php /bin/bash -c "cd /usr/src/php/ext/redis && docker-php-ext-install redis"
docker exec -i my_php /bin/bash -c "docker-php-ext-enable redis"

docker exec -i my_php /bin/bash -c "docker-php-ext-install mysqli"
docker restart my_php

echo "*/1 * * * * sh /home/www/PS_RD/public/cronjob/GitPull.sh
*/1 * * * * docker exec -i my_php /bin/bash -c \"cd /usr/share/nginx/html/PS_RD && php artisan init_available_booking\"" >> /var/spool/cron/root

